package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Project;
import com.manger.model.SuNing;
import com.manger.utils.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SuNingCaseFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025-10-15 9:16
 * @Version 1.0
 */
public class SuNingCaseFilesFormat implements CaseFilesFormat{

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        System.out.println("++++++++++++++++++++++苏宁案件数据整理+++++++++++++++++++++++++++++");
        List<String> ignoreFileList = project.getIgnoreFilesList();
        List<String> ignoreDirList = project.getIgnoreDirList();
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        System.out.println("读取委托信息表：" + excelFilePath);
        Map<String,List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelFilePath), SuNing.class).get();
        if(map.keySet().size() <= 0){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<SuNing> suNingList = (List<SuNing>) map.get(map.keySet().iterator().next());
        if(suNingList == null){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        String parentResourcePath = extractDir + "/" + project.getDebtName() + "/";
        File file = new File(parentResourcePath);
        if(!file.exists()){
            System.out.println("没有检测到文件夹【"+parentResourcePath+"】");
            return;
        }
        Map<String,String> tempMap = new HashMap<String,String>();
        File[] files = file.listFiles();
        for(File tempFile : files){
            File[] mFiles = tempFile.listFiles();
            for(File mFile : mFiles){
                String fileName = mFile.getName();
                SuNing suNing = null;
                for(SuNing suN : suNingList){
                    if(fileName.contains(suN.getIdCard())){
                        suNing = suN;
                        break;
                    }
                }
                if(suNing == null){
                    continue;
                }
                String tempTargetPath = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME
                        + "/" + suNing.getUserName() + "-" + suNing.getIdCard() ;
                CopyFiles.copyFileToDir(mFile.getCanonicalPath(), tempTargetPath + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
                tempMap.put(suNing.getIdCard(),tempTargetPath);
                System.out.println("整理案件【"+suNing.getUserName()+"-"+suNing.getIdCard()+"】");
            }
        }
        //拷贝材料
        System.out.println("整理目标材料");
        CopyFiles.copyOtherMaterialFileToDir(extractDir,tempMap);

        for(String key: tempMap.keySet()){
            String path  = tempMap.get(key);
            CopyFiles.copyBaseFiles(path,project);
        }
    }

    public static void main(String[] args) throws Exception {
        ChinaAreaTools.init();
        ProjectUtils.init();
        SuNingCaseFilesFormat suNingCaseFilesFormat = new SuNingCaseFilesFormat();
        String target = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        String extractDir = "C:\\Users\\liuhj\\Desktop\\苏宁";
        suNingCaseFilesFormat.format(extractDir,target,ProjectUtils.projectMap.get(DetNameEnum.SU_NING));
    }
}
